Raw Source
vyach.vasiliev / Get of extension from Chrome-WebStore [chrome.google.com]

// ==UserScript==
// @name         Get of extension from Chrome-WebStore [chrome.google.com]
// @version      0.3
// @description  Add button for get .crx of extension or theme from Chrome-WebStore
// @description  Bookmarklet version: http://bit.ly/get_crx_chrome_bookmarklet
// @author       Vyacheslav Vasiliev
// @history      0.3 Fix problem with bookmarklet in Chrome-like browsers. Add independent style for the button download.
// @history      0.2 Add new method download
// @include      *chrome.google.com/webstore/*
// @namespace    132-148-320
// @downloadURL  https://cdn.rawgit.com/vyach-vasiliev/d5d879a7e0d7011deff8141789295a6a/raw/cf3fe039468a1262bfdc16b3943ad57858040b20/get_crx_on_chrome_webstore.user.js
// @updateURL    https://cdn.rawgit.com/vyach-vasiliev/d5d879a7e0d7011deff8141789295a6a/raw/cf3fe039468a1262bfdc16b3943ad57858040b20/get_crx_on_chrome_webstore.user.js
// @copyright    © 2017, Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com)
// @run-at       document-end
// ==/UserScript==


(function () {
    var install_button = document.querySelector('[role="dialog"] div[role="button"][aria-label]');
    if (install_button) {
        // var install_button_classes = install_button.className;
        var get_crx_button = document.createElement('a');
        get_crx_button.className = '';
        get_crx_button = install_button.parentNode.insertBefore(get_crx_button, install_button);
        get_crx_button.innerText = "Get .crx file";
        get_crx_button.id = 'get-crx-file';
        var crx_param = getCRXParam();
        var crx_id = crx_param[0];
        var crx_name = crx_param[1];
        var crx_url_download = crx_param[2];
        var crx_url_download_encode = crx_param[3];

        get_crx_button.setAttribute('download', crx_name+'.crx');
        get_crx_button.setAttribute('href', crx_url_download);
        console.log('install href\n%s', crx_url_download);

        checkURL();

        // if not added style
        get_crx_button.style.marginRight = "2px";
        // add style for button
        var get_crx_button_style = '#get-crx-file{margin-right:2px;user-select:none;padding:0 20px 0 20px;-webkit-border-radius:2px;border-radius:2px;border:1px solid;cursor:pointer;color:#fff;font-weight:bold;text-align:center;text-shadow:0 1px 1px rgba(0,0,0,0.1);text-transform:uppercase;-webkit-box-shadow:0 2px 0 0 rgba(255,255,255,.06) inset,0 2px 3px 0 rgba(0,0,0,.2);box-shadow:0 2px 0 0 rgba(255,255,255,.06) inset,0 2px 3px 0 rgba(0,0,0,.2);outline:0;vertical-align:top;height:28px;font-size:11px;line-height:28px;text-decoration:none;display:inline-block;border-color:#2d53af;background-color:#4d7bd6;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#5689db),color-stop(100%,#4d7bd6));background-image:-webkit-linear-gradient(top,#5689db 0,#4d7bd6 100%);background-image:linear-gradient(to bottom,#5689db,#4d7bd6)}#get-crx-file:hover{opacity:.8;border-color:#4076ff;background-color:#5c92ff;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#669eff),color-stop(100%,#5286ff));background-image:-webkit-linear-gradient(top,#669eff 0,#5286ff 100%);background-image:linear-gradient(to bottom,#669eff,#5286ff)}#get-crx-file:focus{border-color:#4076ff;background-color:#5c92ff;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#669eff),color-stop(100%,#5286ff));background-image:-webkit-linear-gradient(top,#669eff 0,#5286ff 100%);background-image:linear-gradient(to bottom,#669eff,#5286ff)}#get-crx-file:active{-webkit-box-shadow:0 2px 3px 0 rgba(0,0,0,.2) inset;box-shadow:0 2px 3px 0 rgba(0,0,0,.2) inset}';
        addStyle(get_crx_button_style);
    }
    function checkURL() {
        // FIX ERROR in CHROME: When executing a script from a bookmarklet, the link is decoded automatically.
        // first check
        var get_srx_button_href = get_crx_button.getAttribute('href');
        console.log('first check href\n%s', get_srx_button_href);
        if(get_srx_button_href.match('id=')){
            get_crx_button.setAttribute('href', crx_url_download_encode);
        }
        // second check
        get_srx_button_href = get_crx_button.getAttribute('href');
        console.log('second check href\n%s', get_srx_button_href);
        if(get_srx_button_href.match('id=')){
            var message = 'Unfortunately, I cannot provide the correct link.\nPlease contact me to discuss this error!\nClick OK to go to the support page.';
            var issue_url = 'https://github.com/vyach-vasiliev/Get_CRX_UserScript/issues';
            if (confirm(message)){
                window.open(issue_url, '_blank');
            }
        }
    }
    function getCRXParam() {
        // console.info('press getCRX');
        var crx_url_download_tmp = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=49.0&x=id%3D~ID_EXTENSION~%26installsource%3Dondemand%26uc';
        var crx_url_download_encode_tmp = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=49.0&x=id%253D~ID_EXTENSION~%2526installsource%253Dondemand%2526uc';

        var crx_id = window.location.pathname.match(/(\w+){28,}/g);
        if (crx_id) {
            crx_id = crx_id[0];
            crx_url_download = crx_url_download_tmp.replace('~ID_EXTENSION~', crx_id);
            crx_url_download_encode = crx_url_download_encode_tmp.replace('~ID_EXTENSION~', crx_id);
            // console.info('Build get .crx of url: %s', crx_url_download);
        }
        var crx_name = document.title.split('-')[0].trim();
        if(!crx_name) crx_name = crx_id;
        return [crx_id, crx_name, crx_url_download, crx_url_download_encode];
    }
    function addStyle(css) {
        var style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css;
        document.head.appendChild(style); // append in head
        // document.body.appendChild(style); // append in body
    }
})();